package xuhaijia.lanou3g.autohome.recommend.video;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.zip.Inflater;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.activity.MainActivity;
import xuhaijia.lanou3g.autohome.base.BaseFragment;
import xuhaijia.lanou3g.autohome.base.NetTool;
import xuhaijia.lanou3g.autohome.base.StringValues;
import xuhaijia.lanou3g.autohome.base.URLValues;
import xuhaijia.lanou3g.autohome.custom.MyListener;
import xuhaijia.lanou3g.autohome.custom.PullToRefreshLayout;
import xuhaijia.lanou3g.autohome.recommend.bulletin.BulletinBean;

/**
 * Created by 7 on 16/8/19.
 */
public class VideoFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ListView videoLv;
    private LinearLayout all;
    private VideoAdapter adapter;
    private TextView allType;
    private VideoBean videoBean;

    public VideoBean getVideoBean() {
        return videoBean;
    }

    public void setVideoBean(VideoBean videoBean) {
        this.videoBean = videoBean;
    }

    @Override
    protected int setLayout() {
        return R.layout.fragment_video;
    }

    @Override
    protected void initView() {
        videoLv = bindView(R.id.video_lv);
        PullToRefreshLayout ptrl = bindView(R.id.fragment_video_ptfl);
        ptrl.setOnRefreshListener(new MyListener(this));
        View headerView = LayoutInflater.from(mContext).inflate(R.layout.fragment_video_header , null);
        all = (LinearLayout) headerView.findViewById(R.id.video_all);
        allType = (TextView) headerView.findViewById(R.id.alltype);
        all.setOnClickListener(this);
        videoLv.addHeaderView(headerView);
        videoLv.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        adapter = new VideoAdapter(mContext);
        mNetTool.getNetData(URLValues.VIDEO_URL, VideoBean.class, new NetTool.NetListener<VideoBean>() {
            @Override
            public void onSuccess(VideoBean videoBean) {
                adapter.setVideoBean(videoBean);
                videoLv.setAdapter(adapter);
                setVideoBean(videoBean);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        MainActivity mainActivity = (MainActivity) getActivity();
        mainActivity.videoDrawer();
        IntentFilter filter = new IntentFilter();
        filter.addAction(StringValues.CHANGETYPE);
        getActivity().registerReceiver(receiver , filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(receiver);
    }

    int count = 30;
    @Override
    public void load() {
//        http://app.api.autohome.com.cn/autov4.8.8/news/videolist-pm1-vt0-s30-lastid0.json
        String urlString = URLValues.VIDEO_URL;
        int start = urlString.indexOf("http");
        int middle = urlString.indexOf("30-l");
        int end = urlString.indexOf("-last");
        count = count + 10;
        String urlStart = urlString.substring(start, middle);
        String urlEnd = urlString.substring(end , urlString.length());
        String urlFinal = urlStart + count + urlEnd;
        mNetTool.getNetData(urlFinal, VideoBean.class, new NetTool.NetListener<VideoBean>() {
            @Override
            public void onSuccess(VideoBean videoBean) {
                adapter.setVideoBean(videoBean);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void refresh() {
        mNetTool.getNetData(URLValues.VIDEO_URL, VideoBean.class, new NetTool.NetListener<VideoBean>() {
            @Override
            public void onSuccess(VideoBean videoBean) {
                adapter.setVideoBean(videoBean);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_all:
                Intent intent = new Intent();
                intent.setAction(StringValues.VIDEO);
                getActivity().sendBroadcast(intent);
                break;
        }
    }
    public void changeType(int position) {
//        http://app.api.autohome.com.cn/autov4.8.8/news/videolist-pm1-vt0-s30-la
        String urlString = URLValues.VIDEO_URL;
        int start = urlString.indexOf("http");
        int middle = urlString.indexOf("0-s");
        int end = urlString.indexOf("-s30");
        String urlStart = urlString.substring(start, middle);
        String urlEnd = urlString.substring(end , urlString.length());
        String urlFinal = urlStart + position + urlEnd;
        mNetTool.getNetData(urlFinal, VideoBean.class, new NetTool.NetListener<VideoBean>() {
            @Override
            public void onSuccess(VideoBean videoBean) {
                adapter.setVideoBean(videoBean);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }
    public BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(StringValues.CHANGETYPE)) {
                int position = intent.getIntExtra("position" , 0);
                changeType(position);
                allType.setText(StringValues.type[position]);
            }
        }
    };


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        VideoBean.ResultBean.ListBean bean = getVideoBean().getResult().getList().get(position);
        Intent intent = new Intent(getContext() , MediaPlayerActivity.class);
        intent.putExtra("bean" , bean);
        startActivity(intent);
    }
}
